//
//  JPDoctorDetailedVC.m
//  用户信息设置
//
//  Created by 周述坚 on 16/11/14.
//  Copyright © 2016年 周述坚. All rights reserved.
//

#import "JPDoctorDetailedVC.h"
#import "JPDetailedHeaderView.h"
#import <SVProgressHUD.h>
#import "InterfaceTool.h"

#define width self.view.bounds.size.width

@interface JPDoctorDetailedVC ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIImageView *customView;
@property (nonatomic,weak) JPDetailedHeaderView *headerView;

@end

@implementation JPDoctorDetailedVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self setupUI];
    self.navigationItem.title = @"医生详情";
}

- (void)setupUI {
    // 右上角的收藏按钮
    UIBarButtonItem *collectBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(collectBtnAction:)];
    collectBtn.tag = 0;
    
    self.navigationItem.rightBarButtonItem = collectBtn;

    // 创建头视图-- 加载xib
    JPDetailedHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"JPDetailedHeaderView" owner:nil options:nil] lastObject];
    [self.view addSubview:headerView];
    self.headerView = headerView;
    self.headerView.doctorDetail = self.doctorDetail;
    // 设置约束
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_topLayoutGuide).offset(0);
        make.right.left.equalTo(self.view).offset(0);
        make.height.mas_equalTo(180);
    }];
    
    // 创建滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor orangeColor];
    scrollView.contentSize = CGSizeMake(width *2, 0);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [scrollView endEditing:NO];
    // 设置代理
    scrollView.delegate = self;
    // 设置约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom).offset(5);
        make.left.right.bottom.equalTo(self.view).offset(0);
    }];
//    [scrollView setNeedsLayout]; // 让约束立即生效
    [scrollView layoutIfNeeded]; // 如果需要的时候就布局
    // CGFloat lableW = self.view.bounds.size.width;
    for (int i = 0; i < 2; i ++) {
        UITextView *textView = [[UITextView alloc] init];
        [scrollView addSubview:textView];
        
        if (i == 0) {
            [[InterfaceTool sharedInterfaceTool] getDoctorConditionSuccess:^(id response) {
                NSDictionary *result = response[@"data"];
                NSString *receivedConditionString = result[@"receiving_setting_extra"];
                
                for (NSString *str in result[@"receiving_settings"]) {
                    receivedConditionString = [receivedConditionString stringByAppendingFormat:@"\n\n%@", str];
                }
                textView.text = receivedConditionString;
                textView.textAlignment = NSTextAlignmentCenter;
            } failure:^(NSError *error) {
                textView.text = @"";
                NSLog(@"%@", error);
            }];
        } else {
            [[InterfaceTool sharedInterfaceTool] getDoctorProfileSuccess:^(id response) {
                NSDictionary *result = response[@"data"];
                textView.text = result[@"introduction"];
            } failure:^(NSError *error) {
                textView.text = @"";
                NSLog(@"%@", error);
            }];
        }
        
        textView.font = [UIFont systemFontOfSize:20];
        [textView setEditable:NO];
        //textView.backgroundColor = [UIColor blueColor];
        
        [textView adjustsFontForContentSizeCategory];
        
        CGFloat lableX = width * i;
        
        textView.frame = CGRectMake(lableX, 0, width, scrollView.bounds.size.height);
        
      //  NSLog(@"scrollView.bounds = %@", NSStringFromCGRect(scrollView.bounds));
    }
    
    // 自定义滚动视图
    _customView = [[UIImageView alloc] init];
    //self.customView = customView;
    [self.view addSubview:_customView];

    CGFloat customViewY = 245;
    _customView.frame = CGRectMake(0, customViewY, width * 0.5, 2);
    
    _customView.image = [UIImage imageNamed:@"link_button_01"];
    
    headerView.leftCompletion = ^{
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(0, customViewY, width * 0.5, 2);
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }];
    };
    
    headerView.rightCompletion = ^{
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(width * 0.5, customViewY, width * 0.5, 2);
            [scrollView setContentOffset:CGPointMake(width, 0) animated:YES];
        }];
    };
}

/**
 收藏按钮的点击方法
 */
- (void)collectBtnAction:(UIBarButtonItem *)sender{
    
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    [SVProgressHUD showWithStatus:@"请等候···"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    if (sender.tag == 0) {
        [[InterfaceTool sharedInterfaceTool] getFollowDoctorSuccess:^(id response) {
          //  NSLog(@"%@",response);
            sender.tag = 1;
            [SVProgressHUD showSuccessWithStatus:@"关注成功!"];
            [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplate"]];
        } failure:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"操作失败!"];
        }];
    } else{
        [[InterfaceTool sharedInterfaceTool] getRemovedDoctorSuccess:^(id response) {
      //      NSLog(@"%@",response);
            sender.tag = 0;
            [SVProgressHUD showSuccessWithStatus:@"取消成功!"];
            [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplateEmpty"]];
        } failure:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"操作失败!"];
        }];
    }
    
}

#pragma mark scrollView的代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //scrollView
    
   CGFloat contentX = scrollView.contentOffset.x;

    CGFloat minus =  contentX - width;
    
    CGFloat value = minus / width;

    CGFloat customViewY = 245;

    if (value == 0.0) {
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(width * 0.5, customViewY, width * 0.5, 2);
        }];
    }else if (value == -1){
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake( 0, customViewY, width * 0.5, 2);
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
